package com.gs.disk.modules.headImgAndWallper.controller.mobile;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.gs.entity.R;
import com.gs.disk.modules.headImgAndWallper.dto.ResourceDto;
import com.gs.disk.modules.headImgAndWallper.service.mobile.MobileResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("wallper/mobile/resource")
@Api(tags = "光速壁纸--资源(头像 或 壁纸)")
@ApiSort(2)
public class MobileResourceController {

    @Resource
    private MobileResourceService mobileResourceService;

    @Resource
    @Lazy
    private MobileResourceController mobileResourceController;

    @GetMapping("list")
    @ApiOperation("资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类名称", name = "categoryId"),
            @ApiImplicitParam(value = "当前页", name = "page"),
            @ApiImplicitParam(value = "数据量", name = "limit"),
    })
    @Cacheable(value = "resourceList", keyGenerator = "myKeyGenerator")
    public R<Map<String, List<ResourceDto>>> list(@RequestParam(required = false) Long categoryId,
                                                 @RequestParam Long page,
                                                 @RequestParam Long limit) {
        Map<String, List<ResourceDto>> set = mobileResourceService.list(categoryId, page, limit);
        return R.ok(set);
    }

    @GetMapping("sortList")
    @ApiOperation("资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类名称", name = "categoryId"),
            @ApiImplicitParam(value = "当前页", name = "page"),
            @ApiImplicitParam(value = "数据量", name = "limit"),
    })
    public R<Map<String, List<ResourceDto>>> sortList(@RequestParam(required = false) Long categoryId,
                                                  @RequestParam Long page,
                                                  @RequestParam Long limit) {
        Map<String, List<ResourceDto>> map = mobileResourceController.list(categoryId, page, limit)
                .getData();

        Set<String> keys = map.keySet();
        keys.forEach(key -> {
            List<ResourceDto> resourceDtoList = map.get(key);
            Collections.shuffle(resourceDtoList);
        });

        return R.ok(map);
    }
    @PostMapping("upload")
    @ApiOperation("上传资源")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "图片分类", name = "categoryName"),
            @ApiImplicitParam(value = "上传的文件", name = "file",  paramType = "form", dataType = "MultipartFile")
    })
    public R uploadResource(@RequestParam("file")MultipartFile file, @RequestParam String categoryName) {
        mobileResourceService.uploadResource(file, categoryName);
        return R.ok();
    }
}
